/**
 * Created by Administrator on 2014/12/4.
 */
(function () {
    var gzjb = function (config) {
        this._config = config;

        this._work_id = this.getConfig("work_id"); // work id
    };
    if (typeof (Clickmed) === "undefined") {
        Clickmed = {};
    }
    Clickmed.gzjb = gzjb;
    gzjb.prototype.init = function () {
        var me = this;
        var user_id = window.localStorage["userId"]
        var otherUrl = gzbgURL + "newsletter/index.php/Android/getAndroidlistdetail/work_id/" + me._work_id + "/user_id/" + user_id
        PostXF(otherUrl, null, function (dat) {
            me._data = dat;
            if (typeof (dat) == "string") {
                me._data = JSON.parse(dat);
            }
            me.draw();
        }, function () {
            Prompt("获取数据失败", 1000)
        });


    };
    gzjb.prototype.draw = function () {
        var me = this;
        console.log(me._data);
        render = this._config.render || d3.select("body");
        var d1 = this._config.body || d3.select("body");
        var width = this.width;
        var scrol = d3.select("body").append("div").classed("scrol", true).style({
            position: "absolute",
            width: "100%",
            height: "100%",
            "background": "#fff",
            left: "0%",
            top: "0%",
            "z-index": 99
        }).attr("id", "scrol");
        scrol.transition().duration(Transiacion).style("left", "0%");
        var warp = scrol.append("div").classed("dt_warp", true).style({
            position: "relative"
        });
        var datong_header = warp.append("div").classed("datong_header", true);

        var go_back = datong_header.append("div").classed("go_back", true);
        go_back.html("<i><img src =../../common/page/images/go_back_ico.png width =16 height=auto></i>返回");
        go_back.on(touch_s, function () {
            $(this).css("background-color", "#005ebf");
        });
        go_back.on(touch_e, function () {
            $(this).css("background-color", "");
            setTimeout(function () {
                d1.selectAll("#scrol").remove();
                //   myScroll.refresh();
                Android.back = TempFunction.back;
            }, 10);

        });
        // 左上角返回按钮动作
        function back1() {

            $(".comList").remove();
            $(".mark").remove();
            setTimeout(function () {
                d1.selectAll("#scrol").remove();
                //   myscroll.refresh();
            }, 100);
            Android.back = TempFunction.back;
        }

        // Android返回键动作
        Android.back = TempFunction.back1 = back1;
        datong_header.append("h1").html(me._taskName || "");
        me.content();
    };
    gzjb.prototype.content = function () {
        var me = this;
        var detailInfo = me._data["detailinfo"] || "";
        var button = me._data["button"] || "";
        var suggestion = me._data["suggestion"] || "";
        var file = me._data["file"] || "";

        if (detailInfo["work_id"])
            me._workId = detailInfo["work_id"];
        if (detailInfo["reviewer"])
            me._reviewer = detailInfo["reviewer"]   //  2014-12-29 增加提交审核人参数

        var dist = 44;
        if (button.length) {
            dist = 100;
        }
        var scrol = d3.select("#scrol .dt_warp").append("div").attr("id", "gzjb").style({
            height: height - dist + "px",
            "over-flow": "hidden"
        });
        var datong_con = scrol.append("div").classed("datong_con datong_con_re", true);
        var datong_con = datong_con.append("div").classed("phone_cotent", true);
        var ul = datong_con.append("ul");
        var li = ul.append("li").classed("b_btm", true).style({
            width: "100%"
        });
        var table = li.append("table").attr("cellpadding", "0").attr('cellspacing', "0").style({
            width: "100%"
        });
        var tr = table.append("tr");
        tr.append("td").html("<div> 期&nbsp;号</div>").style({
            width: "25%"
        });
        tr.append("td").attr("width", "75%").append("div").html(detailInfo["issue"]).classed("extend", true);
        var tr = table.append("tr");
        tr.append("td").html("<div> 报&nbsp;告&nbsp;类&nbsp;型</div>").style({
            width: "25%"
        });
        tr.append("td").attr("width", "75%").append("div").html(detailInfo["type"]).classed("extend", true);
        var tr = table.append("tr");
        tr.append("td").html("<div> 联&nbsp;系&nbsp;单&nbsp;位</div>").style({
            width: "25%"
        });
        tr.append("td").attr("width", "75%").append("div").html(detailInfo["draft_department_name"]).classed("extend", true);
        var tr = table.append("tr");
        tr.append("td").html("<div> 联&nbsp;系&nbsp;电&nbsp;话</div>").style({
            width: "25%"
        });
        tr.append("td").attr("width", "75%").append("div").html(detailInfo["phonenum"]).classed("extend", true);
        if (suggestion.length || objectJudge(suggestion)) {
            me.showSuggestion();
        }
        if (objectJudge(file)) {
            me.showFile();
        }
        if (button.length || objectJudge(button)) {
            me.showButton();
        }
        Yscroll = new iScroll("gzjb", {hScrollbar: false, vScrollbar: false});
    };
    gzjb.prototype.showFile = function () {
        var me = this;
        var file = me._data["file"];
        var fileDiv = d3.select("#gzjb")
            .select(".datong_con")
            .append("div").classed("phone_cotent", true);
        if (file["draft_file"].length) {
            var fileList = fileDiv.append("div").classed("report_title", true).html("正文");
            var newDiv = fileDiv.append("div").classed("newDiv", true)
                .attr("url", file["draft_file"]).attr("fileName", "正文");
            var len = file["draft_file"].split(".").length;
            var extend = file["draft_file"].split(".")[len - 1];
            var img = newDiv.append("img").attr("src", "../../common/page/images/" + extend + ".png").attr({
                width: "25",
                height: "22"

            }).style("margin-left", "15px");
            // 更改正文名字
            var gragh = newDiv.append("p").html("正文")
                .attr("url", file["draft_file"]);
            // 打开正文附件
            newDiv.on("click", function () {
                if (Flag)
                    return;
                Flag = true;
                setTimeout(function () {
                    Flag = false;
                }, 500);
                var url = $(this).attr("url");
                var fileName = $(this).attr("fileName");
                //    url = serverURL + url.substr(1);
                //   Alert(url);
                window.openF(url, fileName);
            });
        }
        if (file["draft_attachement"].length) {
            var draftLen = file["draft_attachement"];
            var attachement = fileDiv.append("div").classed("report_title", true).html("附件");
            for (var i = 0; i < draftLen.length; i++) {
                var newDiv = fileDiv.append("div").classed("newDiv", true)
                    .attr("url", draftLen[i]["filePath"]).attr("fileName", draftLen[i]["fileName"]);
                var len = draftLen[i]["filePath"].split(".").length;
                var extend = draftLen[i]["filePath"].split(".")[len - 1];
                var img = newDiv.append("img").attr("src", "../../common/page/images/" + extend + ".png").attr({
                    width: "25",
                    height: "22"

                }).style("margin-left", "15px");
                var gragh = newDiv.append("p").html(draftLen[i]["fileName"])
                newDiv.on("click", function () {
                    if (Flag)
                        return;
                    Flag = true;
                    setTimeout(function () {
                        Flag = false;
                    }, 500);
                    var url = $(this).attr("url");
                    var fileName = $(this).attr("fileName");
                    //    url = serverURL + url.substr(1);
                    //   Alert(url);
                    window.openF(url, fileName);
                });

            }
        }
        if (file["receipt_attachement"].length) {
            var draftLen = file["receipt_attachement"]
            var attachement = fileDiv.append("div").classed("report_title", true).html("附件");
            for (var i = 0; i < draftLen.length; i++) {
                var newDiv = fileDiv.append("div").classed("newDiv", true)
                    .attr("url", draftLen[i]["filePath"]).attr("fileName", draftLen[i]["fileName"]);
                var len = draftLen[i]["filePath"].split(".").length;
                var extend = draftLen[i]["filePath"].split(".")[len - 1];
                var img = newDiv.append("img").attr("src", "../../common/page/images/" + extend + ".png").attr({
                    width: "25",
                    height: "22"

                }).style("margin-left", "15px");
                var gragh = newDiv.append("p").html(draftLen[i]["fileName"])
                newDiv.on("click", function () {
                    if (Flag)
                        return;
                    Flag = true;
                    setTimeout(function () {
                        Flag = false;
                    }, 500);
                    var url = $(this).attr("url");
                    var fileName = $(this).attr("fileName");
                    //    url = serverURL + url.substr(1);
                    //   Alert(url);
                    window.openF(url, fileName);
                });
            }
        }
    };
    gzjb.prototype.showSuggestion = function () {
        var me = this;
        var suggestion = me._data["suggestion"] || "";
        var suggestionDiv = d3.select("#gzjb")
            .select(".datong_con")
            .append("div").classed("phone_cotent", true);
        if (suggestion["sz_suggestions"] && suggestion["sz_suggestions"].length) {
            var len = suggestion["sz_suggestions"].length;
            var report_title = suggestionDiv.append("div").classed("report_title", true).html("市领导意见");
            for (var i = 0; i < len; i++) {

                var tempString = suggestion["sz_suggestions"][i];
                var content = (tempString["check_comments"] || "")
                    + "[" + tempString["check_role_name"]
                    + " " + tempString["check_time"] + "]";
                var ul = suggestionDiv.append("ul");
                var li = ul.append("li").classed("b_btm", true).style({
                    padding: "5px 0px"
                });

                var dl = li.append("dl");
                dl.append("dd").html(content.replace(/\n/g, "<br>"));
            }
        }
        if (suggestion["msz_suggestions"] && suggestion["msz_suggestions"].length) {
            var len = suggestion["msz_suggestions"].length;
            var report_title = suggestionDiv.append("div").classed("report_title", true).html("秘书长意见");
            for (var i = 0; i < len; i++) {

                var tempString = suggestion["msz_suggestions"][i];
                var content = (tempString["check_comments"] || "")
                    + "[" + tempString["check_role_name"]
                    + " " + tempString["check_time"] + "]";
                var ul = suggestionDiv.append("ul");
                var li = ul.append("li").classed("b_btm", true).style({
                    padding: "5px 0px"
                });

                var dl = li.append("dl");
                dl.append("dd").html(content.replace(/\n/g, "<br>"));
            }
        }
        if (suggestion["qtld_suggestions"] && suggestion["qtld_suggestions"].length) {
            var len = suggestion["qtld_suggestions"].length;
            var report_title = suggestionDiv.append("div").classed("report_title", true).html("其他领导意见");
            for (var i = 0; i < len; i++) {

                var tempString = suggestion["qtld_suggestions"][i];
                var content = (tempString["check_comments"] || "")
                    + "[" + tempString["check_role_name"]
                    + " " + tempString["check_time"] + "]";
                var ul = suggestionDiv.append("ul");
                var li = ul.append("li").classed("b_btm", true).style({
                    padding: "5px 0px"
                });

                var dl = li.append("dl");
                dl.append("dd").html(content.replace(/\n/g, "<br>"));
            }
        }
        if (suggestion["zbyj_suggestions"] && suggestion["zbyj_suggestions"].length) {
            var len = suggestion["zbyj_suggestions"].length;
            var report_title = suggestionDiv.append("div").classed("report_title", true).html("转办意见");
            for (var i = 0; i < len; i++) {

                var tempString = suggestion["zbyj_suggestions"][i];
                var content = (tempString["receipt_contents"] || "")
                    + "[" + tempString["userName"]
                    + " " + tempString["check_time"] + "]";
                var ul = suggestionDiv.append("ul");
                var li = ul.append("li").classed("b_btm", true).style({
                    padding: "5px 0px"
                });

                var dl = li.append("dl");
                dl.append("dd").html(content.replace(/\n/g, "<br>"));
            }
        }
        if (suggestion["cb_suggestions"] && suggestion["cb_suggestions"].length) {
            var len = suggestion["cb_suggestions"].length;
            var report_title = suggestionDiv.append("div").classed("report_title", true).html("委办局意见");
            for (var i = 0; i < len; i++) {

                var tempString = suggestion["cb_suggestions"][i];
                var content = (tempString["receipt_comments"] || "")
                    + "[" + tempString["receipt_role_name"]
                    + " " + tempString["check_time"] + "]";
                var ul = suggestionDiv.append("ul");
                var li = ul.append("li").classed("b_btm", true).style({
                    padding: "5px 0px"
                });

                var dl = li.append("dl");
                dl.append("dd").html(content.replace(/\n/g, "<br>"));
            }
        }

    };
    gzjb.prototype.showButton = function () {
        var me = this;
        var buttonLen = me._data["button"];
        var warp = d3.select("#scrol .dt_warp");
        var button = warp.append("div").classed("tj_button2", true).classed("qb_btn_bg", true);
        $(".tj_button2 p input").remove();
        var tj_button = button.append("p").style({
            "text-align": "center"
        });
        for (var i = 0; i < buttonLen.length; i++) {
            var input = tj_button.append("input").classed("other", true)
                .attr("name", buttonLen[i]["name"]).attr("submitUrl", buttonLen[i]["url"])
                .attr("is_suggestion", buttonLen[i]["is_suggestion"])
                .attr("is_userlist", buttonLen[i]["is_userlist"])
                .attr("is_pc", buttonLen[i]["is_pc"]).attr("type", "button")
                .attr("value", buttonLen[i]["name"]);
            if (buttonLen[i]["is_userlist"] == "1") {
                $(input.node()).data("userlist", buttonLen[i]["userlist"]);
            }
        }
        if ($(".tj_button2 p input").length <= 2) {
            $(".tj_button2 p input").css({
                width: "40%"
            });
        }
        if ($(".tj_button2 p input").length === 3) {
            $(".tj_button2 p input").css({
                width: "26.1%"
            });
        }
        if ($(".tj_button2 p input").length === 4) {
            $(".tj_button2 p input").css({
                width: "20%"
            });
        }
        if ($(".tj_button2 p input").length === 5) {
            $(".tj_button2 p input").css({
                width: "16%",
                "margin-left": "1%",
                "margin-right": "1%"
            });
        }
        me.buttonClick();
    };
    gzjb.prototype.buttonClick = function () {
        var me = this;
        $(".other").on("click", function () {
            if (Flag)
                return;
            Flag = true;
            setTimeout(function () {
                Flag = false;
            }, 500);
            me._name = $(this).attr("name");
            me._is_suggestion = $(this).attr("is_suggestion");
            me._is_userlist = $(this).attr("is_userlist");
            me._is_pc = $(this).attr("is_pc");
            me._submitUrl = $(this).attr("submitUrl");
            if (me._is_userlist == "1") {
                me._userlist = $(this).data("userlist");
            }
            if (me._is_pc == "2") {
                Alert("请在PC端处理");
            }
            else if (me._is_suggestion == "-1" && me._is_userlist == "-1") {
                var url = me._submitUrl;
                var self = {
                    id: me._workId,
                    eventsid: me._todoId,
                    d_r_id: me._businessId
                };
                if (me._name.indexOf("通过") > -1) {
                    self["draft_status"] = "3";
                }
                if (me._name.indexOf("确认收文") > -1) {
                    self = {
                        eventsid: me._todoId,
                        id: me._businessId
                    }
                }
                if (me._name.indexOf("阅毕") > -1) {
                    var self = {
                        id: me._businessId,
                        eventsid: me._todoId
                    }
                }
                if (me._name.indexOf("办结") > -1) {
                    var self = {
                        id: me._workId,
                        eventsid: me._todoId
                    }
                }
                //  2014-12-29 增加提交审核人功能
                if (me._name.indexOf("提交审核人") > -1) {

                    self["draft_status"] = "1";
                    self["reviewer"] = me._reviewer;

                }
                //  2014-12-29 增加提交审核人功能
                PostXF(url, self, function (dat) {
                    console.log(dat)
                    if (dat && dat != '0') {
                        Prompt("提交成功");

                        setTimeout(function () {


                            setTimeout(function () {
                                d3.selectAll("#scrol").remove();
                                // 删除此项 ，刷新列表
                                me._div.remove();
                                iScrollDiv.refresh();

                            }, Transiacion);
                            // Android返回键动作更改并重新刷新待办列表
                            window.localStorage.status = "true";
                            Android.back = TempFunction.back;
                        }, 0);
                    }
                    else {
                        Prompt("提交失败");
                    }
                }, function () {
                    Prompt("提交失败");
                });
            }
            else {
                me.submitWith()
            }
        });
    };
    gzjb.prototype.submitWith = function () {
        var me = this;
        var select = d3.select("body").append("div").classed("select", true).style({
            width: "100%",
            height: "100%",
            background: "#fff",
            position: "absolute",
            left: "0px",
            top: "0px",
            "z-index": 100,
            "over-follow": "hidden"
        });
        var dt_warp = select.append("div").classed("dt_warp", true);
        var head = dt_warp.append("div").classed("datong_header", true);

        var back = head.append("div").classed("go_back", true);
        back.html("<i><img src =../../common/page/images/go_back_ico.png width =16 height=auto></i>返回");
        back.on(touch_s, function () {
            $(this).css("background-color", "#005ebf");
        });
        // 左上角返回按钮 动作
        back.on(touch_e, function () {
            $(this).css("background-color", "");
            setTimeout(function () {
                select.remove();
                Android.back = TempFunction.back1;
            }, 10);
        });
        head.append("h1").html("工作报告");
        var datong_con = dt_warp.append("div").classed("datong_con", true).style({
            height: (height - 44) + "px",
            background: "#fff"
        });
        var dist = 122;
        if (me._is_suggestion == "1") {
            var dist = 285
            var ps_con = datong_con.append("div").classed("ps_con", true);
            var tj_select = ps_con.append("div").classed("tj_select", true).style({
            });
            var tj_con = tj_select.append("div").classed("div_input div_input2", true);
            var label = tj_con.append("label");
            var textarea = $("<textarea></textarea>").attr("rows", "5").addClass("textarea").attr("placeholder", "请填写意见");
            $(tj_con.node()).append(textarea);
        }
        if (me._is_userlist == "1") {

            datong_con.append("div").classed("report_title", true).html("选择收件人").style({
                "text-align": "center"
            });
            var list = datong_con.append("div").classed("list", true).style({
                width: "100%",
                height: (height - dist) + "px",
                "over-follow": "hidden",
                "margin": "0 auto"
            }).attr("id", "list");
            var table = list.append("div").style({
                display: "table",
                width: "100%"
            }).attr("cellpadding", "0").attr("cellspacing", "0");
            for (var i = 0; i < me._userlist.length; i++) {
                if (me._userlist[i]["list"] && me._userlist[i]["list"].length) {
                    var tr = table.append("div").classed("sjt_tit", true).html(me._userlist[i]["name"]);
                    var tr1 = table.append("div").classed("sjr_name", true);
                    var ul = tr1.append("ul");
                    var tempUser = (me._userlist[i]["list"]);
                    for (var j = 0; j < tempUser.length; j++) {
                        var li = ul.append("li").attr("id", tempUser[j]["id"])
                            .attr("name", tempUser[j]["name"]).attr("parentId", tempUser[j]["parentId"]);

                        li.html("<i class= choose_ico></i>" + tempUser[j]["name"]);
                        li.on("click", function () {
                            if (Flag)
                                return;
                            Flag = true;
                            setTimeout(function () {
                                Flag = false;
                            }, 500);
                            if (!$(this).hasClass("sel1")) {
                                $(this).addClass("sel1");
                                d3.select(this).select("i").classed("choosed_ico", true).classed("choose_ico", false);
                            }
                            else {
                                $(this).removeClass("sel1");
                                d3.select(this).select("i").classed("choose_ico", true).classed("choosed_ico", false);
                            }
                        });
                    }
                }

            }
            new iScroll("list", {hScrollbar: false, vScrollbar: false});
        }
        var tj_button = datong_con.append("div").classed("tj_button2", true).classed("qb_btn_bg", true).style({
            bottom: "0px"
        });


        var cancel = tj_button.append("input").classed("btn_back", true).attr("type", "button")
            .attr("value", "取  消");
        var ensure = tj_button.append("input").classed("btn_agree", true).attr("type", "button")
            .attr("value", "确  认");
        cancel.on("click", function () {

            if (Flag)
                return;
            Flag = true;
            setTimeout(function () {
                Flag = false;
            }, 500);
            // 退回到详细页面
            setTimeout(function () {
                select.remove();
                // Android 返回键动作更改
                Android.back = TempFunction.back1;
            }, Transiacion);
        });
        ensure.on("click", function () {
                if (Flag)
                    return;
                Flag = true;
                setTimeout(function () {
                    Flag = false;
                }, 500);
                if (me._is_suggestion == "1" && !$("textarea").val().length) {
                    Prompt("请填写审批意见");
                    return;
                }
                var url = me._submitUrl;
                var self = {
                    id: me._businessId,
                    eventsid: me._todoId,
                    check_comments: $("textarea").val()

                }
                // MOD20141225
                if (me._name.indexOf("回执") > -1) {
                    var self = {
                        id: me._businessId,
                        eventsid: me._todoId,
                        receipt_comments: $("textarea").val()
                    }
                } //
                if (me._name.indexOf("退回") > -1) {
                    var self = {
                        id: me._workId,
                        eventsid: me._todoId,
                        d_r_id: me._businessId,
                        reviewe_content: $("textarea").val()
                    };
                    self["draft_status"] = "2";
                }
                if (me._is_userlist == "1") {
                    if (true) {
                        me._ld_input = [];
                        me._wj_input = [];
                        me._submitName = null;
                        $("#list ul li").each(function () {
                            if ($(this).hasClass("sel1")) {
                                if ($(this).attr("parentId") <= "3") {
                                    me._ld_input.push($(this).attr("id"))
                                }
                                else {
                                    me._wj_input.push($(this).attr("id"))
                                }
                                if (!me._submitName) {
                                    me._submitName = "<br>" + $(this).attr("name");
                                } else {
                                    me._submitName += "<br>" + $(this).attr("name");
                                }
                            }
                        });
                    }
                    var self = {
                        told_input: me._ld_input,
                        towj_input: me._wj_input,
                        pid: "1",
                        receipt_contents: $("textarea").val(),
                        draft_id: me._workId,
                        eventsid: me._todoId

                    }
                    if (me._name == "分发") {
                        var self = {
                            ld_input: me._ld_input,
                            wj_input: me._wj_input,
                            pid: "0",
                            draft_id: me._workId,
                            eventsid: me._todoId

                        }
                    }
                }
                PostXF(url, self, function (dat) {
                    console.log(dat)


                    if (dat && dat != "0") {
                        if (me._is_userlist == "1") {
                            Prompt("成功提交给" + me._submitName);
                        } else {
                            Prompt("提交成功");
                        }
                        setTimeout(function () {
                            select.remove();
                        }, 10);
                        setTimeout(function () {
                            setTimeout(function () {
                                d3.selectAll("#scrol").remove();

                                // 删除此项 ，刷新列表
                                me._div.remove();
                                iScrollDiv.refresh();

                            }, Transiacion);
                            // Android返回键动作更改并重新刷新待办列表
                            window.localStorage.status = "true";
                            Android.back = TempFunction.back;
                        }, 0);
                    } else {
                        Prompt("提交失败");
                    }

                }, function () {
                    Prompt("提交失败");
                });
            }
        )
        ;
        // Android 返回键动作
        function back2() {
            $(".comList").remove();
            $(".mark").remove();
            setTimeout(function () {
                select.remove();
            }, 10);
            Android.back = TempFunction.back1;
        }

        Android.back = TempFunction.back2 = back2;
    };
    gzjb.prototype.getConfig = function (key) {
        if (this._config == null) {
            return null;
        } else {
            if (typeof (this._config[key]) != "undefined") {
                return this._config[key];
            } else {
                return null;
            }
        }
    };
})
()